<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:14 $)
// $RCSfile: admin_languages.php,v $ : $Revision: 1.14 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_html.php");
require_once ("./includes/functions_language.php");
$template->loadLanguageSection("languages");


/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing language data
*/
function _languageFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["isdefault"] = iif($record["isdefault"]==1, $_SWIFT["language"]["yes"], $_SWIFT["language"]["no"]);
	$record["textdirection"] = iif($record["textdirection"]=="rtl", $_SWIFT["language"]["rtl"], $_SWIFT["language"]["ltr"]);
	$record["title"] = '<a href="index.php?_m=core&_a=managephrases&languageid='. intval($record["languageid"]) .'">'. htmlspecialchars($record["title"]) .'</a>';

	$record["options"] = '<a href="index.php?_m=core&_a=managephrases&languageid='. $record["languageid"] .'" title="'. $_SWIFT["language"]["phrases"] .'"><img src="'. $_SWIFT["themepath"] .'icon_phrases.gif" border="0">&nbsp;'. $_SWIFT["language"]["phrases"] .'</a>&nbsp;&nbsp;&nbsp;<a href="index.php?_m=core&_a=editlanguage&languageid='. $record["languageid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>'. iif($record["ismaster"]==1, '&nbsp;&nbsp;&nbsp;&nbsp;<img src="'. $_SWIFT["themepath"] .'icon_deletedisabled.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=core&_a=managelanguages&do=delete&languageid='. $record["languageid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;');
	return $record;
}

/**
* Mass Action: Delete Languages
*/
function _maDeleteLanguages()
{
	global $_SWIFT;

	if (!is_array($_POST["itemid"]) || $_SWIFT["isdemo"] == true)
	{
		return false;
	}

	deleteLanguages($_POST["itemid"]);
}



/**
* ###############################################
* MANAGE LANGUAGES
* ###############################################
*/
if ($eventaction == "managelanguages")
{
	if ($_GET["do"] == "delete" && $_GET["languageid"] != "" && $_SWIFT["isdemo"] != true)
	{
		if (isset($_SWIFT["languagecache"][$_GET["languageid"]]))
		{
			$infomessage = sprintf($_SWIFT["language"]["languagedeleteconfirmation"], $_SWIFT["languagecache"][$_GET["languageid"]]["title"]);
			deleteLanguages(array($_GET["languageid"]));
		}
	}

	if (trim($_REQUEST["insertlanguageid"]) != "")
	{
		$infomessage = sprintf($_SWIFT["language"]["languageinsertconfirmation"], $_SWIFT["languagecache"][$_REQUEST["insertlanguageid"]]["title"]);
	} else if ($_REQUEST["updatelanguageid"] != "") {
		$infomessage = sprintf($_SWIFT["language"]["languageupdateconfirmation"], $_SWIFT["languagecache"][$_REQUEST["updatelanguageid"]]["title"]);
	}
	

	$options["recordsperpage"] = "6";
	$options["sortby"] = "title";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteLanguages";

	$options["idname"] = "languageid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["languagetitle"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'languages` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;';
	$options["advancedsearch"][1]["title"] = $_SWIFT["language"]["isocode"];
	$options["advancedsearch"][1]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'languages` WHERE `languagecode` LIKE \'%$_searchstr%\' $_sortjoin;';
	$options["advancedsearch"][2]["title"] = $_SWIFT["language"]["languagecharset"];
	$options["advancedsearch"][2]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'languages` WHERE `charset` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_languageFields";

	$fields[0]["name"] = "title";
	$fields[0]["title"] = $_SWIFT["language"]["languagetitle"];
	$fields[0]["width"] = "";

/*	$fields[1]["name"] = "isdefault";
	$fields[1]["title"] = $_SWIFT["language"]["isdefault"];
	$fields[1]["width"] = "100";
	$fields[1]["align"] = "center";*/

	$fields[1]["name"] = "textdirection";
	$fields[1]["title"] = $_SWIFT["language"]["textdirection"];
	$fields[1]["width"] = "120";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "languagecode";
	$fields[2]["title"] = $_SWIFT["language"]["isocode"];
	$fields[2]["width"] = "100";
	$fields[2]["align"] = "center";

	$fields[3]["name"] = "charset";
	$fields[3]["title"] = $_SWIFT["language"]["languagecharset"];
	$fields[3]["width"] = "120";
	$fields[3]["align"] = "center";

	$fields[4]["type"] = "custom";
	$fields[4]["name"] = "options";
	$fields[4]["title"] = $_SWIFT["language"]["options"];
	$fields[4]["width"] = "240";
	$fields[4]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["languages"]." > ".$_SWIFT["language"]["manage"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=managelanguages" title="'.$_SWIFT["language"]["languages"].'">'.$_SWIFT["language"]["languages"].'</a>', $_SWIFT["language"]["desc_languages"] , 1);
	printInfoBox($infomessage);

	$grid->start("languages", $_SWIFT["language"]["languagelist"], 'SELECT * FROM `'. TABLE_PREFIX .'languages` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'languages`;', 'SELECT * FROM `'. TABLE_PREFIX .'languages` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	
	$grid->display("languages");
	$interface->adminFooter();




/**
* ###############################################
* INSERT LANGUAGE
* ###############################################
*/
} else if ($eventaction == "insertlanguage") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["author"]) == "" || trim($_POST["languagecode"]) == "" || trim($_POST["charset"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			// Insert the new language

			$_POST["displayorder"] = iif(empty($_POST["displayorder"]), "1", $_POST["displayorder"]);	

			$languageid = insertNewLanguage($_POST["title"], $_POST["author"], false, $_POST["textdirection"], $_POST["languagecode"], $_POST["charset"], $_POST["displayorder"]);

			printRedirect(sprintf($_SWIFT["language"]["languageinsertconfirmation"], $_POST["title"]), "index.php?_m=core&_a=managelanguages&insertlanguageid=".$languageid);
			exit;
		}
	}

	$interface->adminHeader($_SWIFT["language"]["languages"]." > ".$_SWIFT["language"]["insert"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=managelanguages" title="'.$_SWIFT["language"]["languages"].'">'.$_SWIFT["language"]["languages"].'</a> &raquo; <a href="index.php?_m=core&_a=insertlanguage" title="'.$_SWIFT["language"]["insert"].'">'.$_SWIFT["language"]["insert"].'</a>', $_SWIFT["language"]["desc_languages"] , 1);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderLanguageForm(INSERT);

	$template->assign("backurl", "index.php?_m=core&_a=managelanguages");
	$interface->adminFooter();






/**
* ###############################################
* EDIT LANGUAGE
* ###############################################
*/
} else if ($eventaction == "editlanguage") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["author"]) == "" || trim($_POST["languagecode"]) == "" || trim($_POST["charset"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			// Insert the new language

			$_POST["displayorder"] = iif(empty($_POST["displayorder"]), "1", $_POST["displayorder"]);	

			updateLanguage($_POST["languageid"], $_POST["title"], $_POST["author"], false, $_POST["textdirection"], $_POST["languagecode"], $_POST["charset"], $_POST["displayorder"]);

			printRedirect(sprintf($_SWIFT["language"]["languageupdateconfirmation"], $_POST["title"]), "index.php?_m=core&_a=managelanguages&updatelanguageid=".$_POST["languageid"]);
			exit;
		}
	}

	$_language = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."languages` WHERE `languageid` = '". $dbCore->escape($_GET["languageid"]) ."';");
	if (empty($_language["languageid"]))
	{
		trigger_error($_SWIFT["language"]["invalidlanguageid"], E_USER_ERROR);
	}
	$_POST = array_merge($_POST, $_language);

	$interface->adminHeader($_SWIFT["language"]["languages"]." > ".$_SWIFT["language"]["edit"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=managelanguages" title="'.$_SWIFT["language"]["languages"].'">'.$_SWIFT["language"]["languages"].'</a> &raquo; <a href="index.php?_m=core&_a=editlanguage&languageid='. intval($_language["languageid"]) .'" title="'.htmlspecialchars($_language["title"]).'">'. htmlspecialchars($_language["title"]).'</a>', $_SWIFT["language"]["desc_languages"] , 1);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderLanguageForm(EDIT);

	$template->assign("backurl", "index.php?_m=core&_a=managelanguages");
	$interface->adminFooter();








/**
* ###############################################
* Import/Export Languages
* ###############################################
*/
} else if ($eventaction == "importexportlanguage") {
	require_once ("./includes/functions_xml.php");
	$xml = new XML();

	if ($_POST["export"] == "1")
	{
		doExportLanguage($_POST["languageid"]);
		exit;
	} else if ($_POST["import"] == "1") {
		// Check for successfull upload
		if (file_exists($_FILES["languagefile"]["tmp_name"]) && $_SWIFT["isdemo"] != true)
		{
			// Success.. are we supposed to insert a new language or..?
			if (!empty($_POST["languageid"]))
			{
				// Merge
				$_language = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."languages` WHERE `languageid` = '". intval($_POST["languageid"]) ."';");
				if (empty($_language["languageid"]))
				{
					trigger_error($_SWIFT["language"]["invalidlanguageid"], E_USER_ERROR);
				}

				$result = mergeLanguageFromFile($_POST["languageid"], $_FILES["languagefile"]["tmp_name"], iif($_POST["ignoreversion"]=="1", false, true));
				if ($result == -1)
				{
					$errormessage = $_SWIFT["language"]["versioncheckfailed"];
				} else {
					rebuildPhraseCache($_POST["languageid"]);

					printRedirect(sprintf($_SWIFT["language"]["languageimportconfirmation"], $_language["title"]), "index.php?_m=core&_a=managelanguages&importlanguagetitle=".urlencode($_language["title"]));
					exit;
				}
			} else {
				// New Language
				$_language = importLanguageFromFile($_FILES["languagefile"]["tmp_name"], false, false, 0, iif($_POST["ignoreversion"]=="1", false, true));
				if ($_language == -1)
				{
					$errormessage = $_SWIFT["language"]["versioncheckfailed"];
				} else {
					rebuildLanguageCache();
					rebuildPhraseCache($_language["languageid"]);

					printRedirect(sprintf($_SWIFT["language"]["languageimportconfirmation"], $_language["title"]), "index.php?_m=core&_a=managelanguages&importlanguagetitle=".urlencode($_language["title"]));
					exit;
				}
			}
		}
	}
	

	$interface->adminHeader($_SWIFT["language"]["languages"]." > ".$_SWIFT["language"]["importexport"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=managelanguages" title="'.$_SWIFT["language"]["languages"].'">'.$_SWIFT["language"]["languages"].'</a> &raquo; <a href="index.php?_m=core&_a=importexportlanguage" title="'.$_SWIFT["language"]["importexport"].'">'.$_SWIFT["language"]["importexport"].'</a>', $_SWIFT["language"]["desc_languages"] , 1);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	// ======= EXPORT LANGUAGE =======
	printFormStart();
	printMainTableHeader($_SWIFT["language"]["exportlanguage"]);

	$index = 0;
	foreach ($_SWIFT["languagecache"] as $key=>$val)
	{
		$options[$index]["title"] = $val["title"];
		$options[$index]["value"] = $val["languageid"];
		$index++;
	}

	printSelectRow("languageid", $_SWIFT["language"]["explanguage"], $_SWIFT["language"]["desc_explanguage"], $options);

	printSubmitRow($_SWIFT["language"]["exportxml"]);

	printMainTableFooter();

	addHiddenField("_m", "core");
	addHiddenField("_a", "importexportlanguage");
	addHiddenField("export", "1");
	printFormEnd();


	// ======= IMPORT LANGUAGE =======
	echo "<BR />";
	printFormStart("importform", "index.php", "POST", true);
	printMainTableHeader($_SWIFT["language"]["importlanguage"]);

	printUploadRow("languagefile", $_SWIFT["language"]["languagefile"], $_SWIFT["language"]["desc_languagefile"]);

	$options[0]["title"] = $_SWIFT["language"]["createnewlanguage"];
	$options[0]["value"] = "0";
	$options[0]["selected"] = true;
	$index = 1;
	foreach ($_SWIFT["languagecache"] as $key=>$val)
	{
		$options[$index]["title"] = $val["title"];
		$options[$index]["value"] = $val["languageid"];
		$index++;
	}


	printSelectRow("languageid", $_SWIFT["language"]["mergewith"], $_SWIFT["language"]["desc_mergewith"], $options);

	printYesNoRow("ignoreversion", $_SWIFT["language"]["ignoreversion"], $_SWIFT["language"]["desc_ignoreversion"], iif(empty($_POST["ignoreversion"]), false, $_POST["ignoreversion"]));

	printSubmitRow($_SWIFT["language"]["importxml"]);

	printMainTableFooter();

	addHiddenField("_m", "core");
	addHiddenField("_a", "importexportlanguage");
	addHiddenField("import", "1");
	printFormEnd();
	
	$template->assign("backurl", "index.php?_m=core&_a=managelanguages");
	$interface->adminFooter();







/**
* ###############################################
* MANAGE PHRASES
* ###############################################
*/
} else if ($eventaction == "managephrases") {
	if (empty($_REQUEST["languageid"]))
	{
		$_REQUEST["languageid"] = getMasterLanguageID();
	}

	if (!empty($_REQUEST["phraseinsert"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["phraseinsertconfirm"], htmlspecialchars($_REQUEST["phraseinsert"]));
	} else if (!empty($_REQUEST["phrasedelete"])) {
		$infomessage = sprintf($_SWIFT["language"]["phrasedeleteconfirm"], htmlspecialchars($_REQUEST["phrasedelete"]));
	}
	


	// ======= Fetch the language data =======
	$_language = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."languages` WHERE `languageid` = '". intval($_REQUEST["languageid"]) ."';");
	if (empty($_language["languageid"]))
	{
		trigger_error($_SWIFT["language"]["invalidlanguageid"], E_USER_ERROR);
	}
	
	// ======= Is an update required? =======
	if ($_POST["update"] == 1 && is_array($_POST["phrase"]) && count($_POST["phrase"]) && $_SWIFT["isdemo"] != true)
	{
		updatePhrases($_language["languageid"], $_POST["phrase"]);
		$infomessage = $_SWIFT["language"]["phraseupdateconfirm"];
	}


	// Get the current offset for this page
	if (!empty($_REQUEST["offset"]) && is_numeric($_REQUEST["offset"]))
	{
		$_offset = $_REQUEST["offset"];
	} else {
		$_offset = "0";
	}

	$_limit = 5;

	if ($_REQUEST["_search"] == "1" && trim($_REQUEST["_type"]) == "codetext")
	{
		// Search both code and text?
		$countquery = "SELECT COUNT(*) AS totalitems FROM `". TABLE_PREFIX ."languagephrases` WHERE `languageid` = '". intval($_language["languageid"]) ."' AND (`code` LIKE '%". $dbCore->escape($_REQUEST["_query"]) ."%' OR `contents` LIKE '%". $dbCore->escape($_REQUEST["_query"]) ."%');";
		$fetchquery = " AND (`code` LIKE '%". $dbCore->escape($_REQUEST["_query"]) ."%' OR `contents` LIKE '%". $dbCore->escape($_REQUEST["_query"]) ."%')";
	} else if ($_REQUEST["_search"] == "1" && trim($_REQUEST["_type"]) == "code") {
		// Search just the code?
		$countquery = "SELECT COUNT(*) AS totalitems FROM `". TABLE_PREFIX ."languagephrases` WHERE `languageid` = '". intval($_language["languageid"]) ."' AND (`code` LIKE '%". $dbCore->escape($_REQUEST["_query"]) ."%');";
		$fetchquery = " AND (`code` LIKE '%". $dbCore->escape($_REQUEST["_query"]) ."%')";
	} else {
		$countquery = "SELECT COUNT(*) AS totalitems FROM `". TABLE_PREFIX ."languagephrases` WHERE `languageid` = '". intval($_language["languageid"]) ."';";
		unset($fetchquery);
	}

	// Get the total phrase count
	$_total = $dbCore->queryFetch($countquery);

	// ======= DO WE NEED TO INSERT A NEW PHRASE?? =======
	if ($_POST["addphrase"] == 1) {
		if (trim($_POST["phrasecode"]) == "" || trim($_POST["phrasevalue"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
			$addphrasestyle = "block";
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
			$addphrasestyle = "block";
		} else {
			insertPhrase($_language["languageid"], $_POST["phrasecode"], $_POST["phrasevalue"]);
			$lastoffset = getLastPageOffset($_total["totalitems"]+1, 5);

			printRedirect(sprintf($_SWIFT["language"]["phraseinsertconfirm"], htmlspecialchars($_POST["phrasecode"])), "index.php?_m=core&_a=managephrases&languageid=".$_language["languageid"]."&offset=".intval($lastoffset)."&phraseinsert=".urlencode($_POST["phrasecode"]));
			exit;
		}

	// ======= DO WE NEED TO DELETE A PHRASE? =======
	} else if ($_GET["deletephrase"] == 1 && !empty($_GET["phraseid"]) && $_SWIFT["isdemo"] != true) {
		// Get the phrase data

		$phrasecode = deletePhrase($_GET["phraseid"]);
		if (!$phrasecode)
		{
			$errormessage = $_SWIFT["language"]["phrasedelfailure"];
		} else {
			$lastoffset = getLastPageOffset($_total["totalitems"]-1, 5);

			printRedirect(sprintf($_SWIFT["language"]["phrasedeleteconfirm"], htmlspecialchars($phrasecode)), "index.php?_m=core&_a=managephrases&languageid=".$_language["languageid"]."&offset=".intval($lastoffset)."&phrasedelete=".urlencode($phrasecode));
			exit;
		}
	}
	
	// is the offset wrong?
	$lastoffset = getLastPageOffset($_total["totalitems"], 5);
	if ($_offset > $lastoffset && $lastoffset)
	{
		$_offset = $lastoffset;
	}
	if ($_offset < 0)
	{
		$_offset = 0;
	}

	$interface->adminHeader($_SWIFT["language"]["languages"]." > ".$_SWIFT["language"]["managephrases"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=managelanguages" title="'.$_SWIFT["language"]["languages"].'">'.$_SWIFT["language"]["languages"].'</a> &raquo; <a href="index.php?_m=core&_a=editlanguage&languageid='. intval($_language["languageid"]) .'" title="'.htmlspecialchars($_language["title"]).'">'.htmlspecialchars($_language["title"]).'</a> &raquo; <a href="index.php?_m=core&_a=managephrases&languageid='. intval($_language["languageid"]) .'" title="'.$_SWIFT["language"]["managephrases"].'">'.$_SWIFT["language"]["managephrases"].'</a>', $_SWIFT["language"]["desc_languages"] , 1);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	// ======= ADD PHRASE SECTION =======
	echo '<div id="addphrase" style="DISPLAY:'. iif(!empty($addphrasestyle), $addphrasestyle, 'none') .';">'.SWIFT_CRLF;
	printFormStart();
	printMainTableHeader($_SWIFT["language"]["addphrase"]);
	printTextRow("phrasecode", $_SWIFT["language"]["code"], $_SWIFT["language"]["desc_phrasecode"], "text", $_POST["phrasecode"]);
	printTextareaRow("phrasevalue", $_SWIFT["language"]["value"], $_SWIFT["language"]["desc_phrasevalue"], $_POST["phrasevalue"], "50", "2");

	printSubmitRow($_SWIFT["language"]["insert"]);

	printMainTableFooter();
	addHiddenField("_m", "core");
	addHiddenField("_a", "managephrases");
	addHiddenField("addphrase", "1");
	addHiddenField("languageid", $_language["languageid"]);
	addHiddenField("offset", $_offset);
	addHiddenField("_search", $_REQUEST["_search"]);
	addHiddenField("_type", $_REQUEST["_type"]);
	addHiddenField("_query", $_REQUEST["_query"]);
	printFormEnd();
	echo '<BR /></div>'.SWIFT_CRLF;

	// ======= LANGUAGE JUMP =======
	echo '<div id="changelanguage" style="DISPLAY:none;">'.SWIFT_CRLF;
	printFormStart();
	printMainTableHeader($_SWIFT["language"]["languagejump"]);

	$options = array();
	$index = 0;
	foreach ($_SWIFT["languagecache"] as $key=>$val)
	{
		$options[$index]["title"] = $val["title"];
		$options[$index]["value"] = $val["languageid"];
		if ($val["languageid"] == $_language["languageid"])
		{
			$options[$index]["selected"] = true;
		}
		$index++;
	}
	printSelectRow("languageid", $_SWIFT["language"]["language"], "", $options);
	printSubmitRow($_SWIFT["language"]["jump"]);
	printMainTableFooter();
	addHiddenField("_m", "core");
	addHiddenField("_a", "managephrases");
	addHiddenField("offset", $_offset);
	addHiddenField("_search", $_REQUEST["_search"]);
	addHiddenField("_type", $_REQUEST["_type"]);
	addHiddenField("_query", $_REQUEST["_query"]);
	printFormEnd();
	echo '<BR /></div>'.SWIFT_CRLF;	
	
	$pagination = pagination("index.php?_m=core&_a=managephrases&_search=". urlencode($_REQUEST["_search"]) ."&_type=". urlencode($_REQUEST["_type"]) ."&_query=". urlencode($_REQUEST["_query"]) ."&languageid=".intval($_language["languageid"]), $_total["totalitems"], $_limit, $_offset, "5", "pageoftotal", TRUE, FALSE, TRUE);

	echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">'.SWIFT_CRLF;

	// ======= START PAGINATION TABLE =======
	$paginationdata = '<tr><td><table width="100%"  border="0" cellspacing="0" cellpadding="0"><tr valign="top"><td align="left">'. iif($pagination, '<table border="0" cellpadding="0" cellspacing="1" class="tborder"><tr>'.$pagination.'</tr></table>', '&nbsp;'). '</td></tr></table>'.SWIFT_CRLF;
	$paginationdata .= '<tr><td><img src="'. $_SWIFT["themepath"] .'space.gif" width="1" height="6" /></tr></td>'.SWIFT_CRLF;
	echo $paginationdata;
	// ======= END PAGINATION TABLE =======
	echo '<tr><td>'.SWIFT_CRLF;




	// ======= START MAIN PHRASE RENDERING =======
	printFormStart();
	printMainTableHeader($_language["title"]);

	// ======= TOOLBAR OPTIONS =======
	echo '<tr>'.SWIFT_CRLF;
	echo '<td class="toolbarrow" align="left" colspan="2" valign="middle" nowrap><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="5"><img src="'. $_SWIFT["themepath"] .'grip.gif" border="0" /></td><td class="toolbarrow"><span class="toolbartext">&nbsp;<a href="javascript:switchDisplay(\'addphrase\');" id="white" title="'. $_SWIFT["language"]["addphrase"] .'">'. $_SWIFT["language"]["addphrase"] .'</a> | <a href="javascript:switchDisplay(\'changelanguage\');" id="white" title="'. $_SWIFT["language"]["changelanguage"] .'">'. $_SWIFT["language"]["changelanguage"] .'</a></span></td></tr></table></td>'.SWIFT_CRLF;
	echo '</tr>'.SWIFT_CRLF;
	// ======= TOOLBAR OPTIONS =======

	$colarray[0]["align"] = "left";
	$colarray[0]["nowrap"] = true;
	$colarray[0]["value"] = $_SWIFT["language"]["code"];
	$colarray[0]["width"] = "300";

	$colarray[1]["align"] = "center";
	$colarray[1]["nowrap"] = true;
	$colarray[1]["value"] = $_SWIFT["language"]["value"];

	printDataRow($colarray, "tabletitlerow");

	// Fetch all the phrases
	$phraselist = $phrasecodelist = $masterphraselist = $masterlist = array();
	$dbCore->query("SELECT `code` FROM `". TABLE_PREFIX ."languagephrases` WHERE `languageid` = '". intval($_language["languageid"]) ."'". $fetchquery ." ORDER BY `phraseid`;");
	while ($dbCore->nextRecord())
	{
		$phrasecodelist[] = $dbCore->Record["code"];
	}
	$pcodestr = buildIN($phrasecodelist);

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."languagephrases` WHERE `languageid` = '". intval($_language["languageid"]) ."' AND `code` IN (". $pcodestr .") ORDER BY `code` ASC LIMIT ". $dbCore->escape($_offset) .", ". $dbCore->escape($_limit) .";");
	while ($dbCore->nextRecord())
	{
		$phraselist[$dbCore->Record["code"]] = $dbCore->Record;
		$fcodelist[] = $dbCore->Record["code"];
	}
	

	// Now fetch the same variables from master language
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."languagephrases` WHERE `code` IN (". buildIN($fcodelist) .") AND `languageid` = '". $dbCore->escape(getMasterLanguageID()) ."'". $fetchquery ." ORDER BY `code` ASC;");
	while ($dbCore->nextRecord())
	{
		$masterlist[$dbCore->Record["code"]] = $dbCore->Record["contents"];
	}

	foreach ($phraselist as $key=>$val)
	{
		$value = '<textarea name="phrasedisabled['. $key .']" rows="2" cols="50" class="swifttextdisabled" disabled>'. htmlspecialchars(iif(empty($masterlist[$key]), $_SWIFT["language"]["novalue"], $masterlist[$key])) .'</textarea><BR /><textarea name="phrase['. $key .']" rows="2" cols="50" class="swifttextarea">'. htmlspecialchars($val["contents"]) .'</textarea>';
		printDefaultDescRow($key.'&nbsp;<a href="#" onClick="javascript:doConfirm(\''. $_SWIFT["language"]["phrasedeletepopup"] .'\', \'index.php?_m=core&_a=managephrases&_search='. urlencode($_REQUEST["_search"]) .'&_type='. urlencode($_REQUEST["_type"]) .'&_query='. urlencode($_REQUEST["_query"]) .'&languageid='. intval($_REQUEST["languageid"]) .'&offset='. intval($_REQUEST["offset"]) .'&deletephrase=1&phraseid='. intval($val["phraseid"]) .'\');" title="'. $_SWIFT["language"]["deletephrase"] .'"><img src="'. $_SWIFT["themepath"] .'icon_deletesmall.gif" border="0"></a>', "", $value, "smalltext");		
	}

	printSubmitRow($_SWIFT["language"]["update"]);


	printMainTableFooter();

	addHiddenField("_m", "core");
	addHiddenField("_a", "managephrases");
	addHiddenField("update", "1");
	addHiddenField("languageid", $_language["languageid"]);
	addHiddenField("offset", $_offset);
	addHiddenField("_search", $_REQUEST["_search"]);
	addHiddenField("_type", $_REQUEST["_type"]);
	addHiddenField("_query", $_REQUEST["_query"]);
	printFormEnd();

	// ======= END MAIN PHRASE RENDERING =======
	echo '</tr></td>'.SWIFT_CRLF;
	echo '<tr><td><img src="'. $_SWIFT["themepath"] .'space.gif" width="1" height="6" /></tr></td>'.SWIFT_CRLF;
	echo $paginationdata;
	echo '</table>'.SWIFT_CRLF;



	
	$template->assign("backurl", iif($_REQUEST["_search"]==1, "index.php?_m=core&_a=searchlanguage", "index.php?_m=core&_a=managelanguages"));
	$interface->adminFooter();




/**
* ###############################################
* SEARCH PHRASES
* ###############################################
*/
} else if ($eventaction == "searchlanguage") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["query"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			printRedirect($_SWIFT["language"]["languagesearching"], "index.php?_m=core&_a=managephrases&_search=1&_type=".$_POST["type"]."&_query=".urlencode($_POST["query"])."&languageid=".urlencode($_POST["languageid"]));
			exit;
		}
	}

	$interface->adminHeader($_SWIFT["language"]["languages"]." > ".$_SWIFT["language"]["searchphrases"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=managelanguages" title="'.$_SWIFT["language"]["languages"].'">'.$_SWIFT["language"]["languages"].'</a> &raquo; <a href="index.php?_m=core&_a=searchlanguage" title="'.$_SWIFT["language"]["searchphrases"].'">'.$_SWIFT["language"]["searchphrases"].'</a>', $_SWIFT["language"]["desc_languages"] , 1);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["searchphrases"]);

	printTextRow("query", $_SWIFT["language"]["query"], $_SWIFT["language"]["desc_query"], "text", $_POST["query"]);

	$options[0]["title"] = $_SWIFT["language"]["codetext"];
	$options[0]["value"] = "codetext";
	$options[1]["title"] = $_SWIFT["language"]["code"];
	$options[1]["value"] = "code";

	printSelectRow("type", $_SWIFT["language"]["searchtype"], $_SWIFT["language"]["desc_searchtype"], $options);

	unset($options);
	$index = 0;
	foreach ($_SWIFT["languagecache"] as $key=>$val)
	{
		$options[$index]["title"] = $val["title"];
		$options[$index]["value"] = $val["languageid"];
		$index++;
	}

	printSelectRow("languageid", $_SWIFT["language"]["searchlanguage"], $_SWIFT["language"]["desc_searchlanguage"], $options);

	printSubmitRow($_SWIFT["language"]["search"]);

	printMainTableFooter();

	addHiddenField("_m", "core");
	addHiddenField("_a", "searchlanguage");
	addHiddenField("step", "1");
	if ($type == EDIT)
	{
		addHiddenField("languageid", $_POST["languageid"]);
	}
	printFormEnd();

	$template->assign("backurl", "index.php?_m=core&_a=managelanguages");
	$interface->adminFooter();	

}
?>